package com.webchat.sso.controller;

import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.domain.vo.request.UserLoginInfoRequestVO;
import com.webchat.domain.vo.request.UserRegistryInfoRequestVO;
import com.webchat.rmi.sso.OauthServiceClient;
import com.webchat.sso.service.SSOCoreService;
import com.webchat.sso.service.ValidCodeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sso-service")
public class SSOServiceController implements OauthServiceClient {

    @Autowired
    private SSOCoreService ssoCoreService;
    @Autowired
    private ValidCodeService validCodeService;
    @Autowired
    private HttpServletResponse httpServletResponse;

    @PostMapping("/oauth/login")
    public APIResponseBean<String> login(@RequestBody UserLoginInfoRequestVO loginInfoRequest) {

        return APIResponseBeanUtil.success(ssoCoreService.login(loginInfoRequest, httpServletResponse));
    }

    /**
     * 用户注册
     * @param request
     * @return
     */
    @PostMapping("/oauth/register")
    public APIResponseBean register(@RequestBody UserRegistryInfoRequestVO request) {
        // 数字验证码核验
        validCodeService.validCode(request.getPicCheckCode());
        boolean result = ssoCoreService.registry(request);
        return APIResponseBeanUtil.success(result ? "注册成功" : "测试失败");
    }

    @GetMapping("/oauth/userId/{oauthCode}")
    @Override
    public APIResponseBean<String> getUserIdByOauthCode(@PathVariable String oauthCode) {
        String userId = ssoCoreService.getUserIdByOauthCode(oauthCode);
        return APIResponseBeanUtil.success(userId);
    }

    @GetMapping("/token/userId/{token}")
    @Override
    public APIResponseBean<String> getUserIdByToken(@PathVariable String token) {
        String userId = ssoCoreService.getUserIdByToken(token);
        return APIResponseBeanUtil.success(userId);
    }
}
